<?php
include_once __DIR__.'/WishCrawler.php';
include_once __DIR__.'/WishTicketSender.php';
include_once __DIR__.'/Email.php';

class Helper {
    public static function recentTickets() {
        $ticketIds = array();
        $repliedTickets = WishCrawler::crawlRepliedTickets();
        $ticketOrders = WishCrawler::crawlTicketOrders();
        if (!empty($repliedTickets)) {
            foreach ($repliedTickets as $repliedTicket) {
                $ticketIds[$repliedTicket['id']] = $repliedTicket['id'];
            }
        }
        if (!empty($ticketOrders)) {
            foreach ($ticketOrders as $order) {
                $ticketIds[$order['ticket_id']] = $order['ticket_id'];
            }
        }

        $recentTickets = array();
        foreach ($ticketIds as $ticketId) {
            $ticket = WishCrawler::crawlTicket($ticketId);
            if (!self::isWarnedTicket($ticket)) {
              continue;
            }
            $recentTickets[$ticketId] = $ticket;
        }
        return $recentTickets;
    }

    public static function specifiedTickets($ticketIds) {
        $specifiedTickets = array();
        foreach ($ticketIds as $ticketId) {
            $ticket = WishCrawler::crawlTicket($ticketId);
            $specifiedTickets[$ticketId] = $ticket;
        }
        return $specifiedTickets;   
    }

    private static function isWarnedTicket($ticket) {
        if (trim($ticket['label']) == 'Change Shipping Address'
            || trim($ticket['label']) == 'Shipping Status or ETA Inquiry') {
            return false;
        }

        if (count($ticket['replies']) > 1) {
            $haveWishSupport = false;
            foreach ($ticket['replies'] as $ticketReply) {
                if (trim($ticketReply['sender']) == 'Wish Support') {
                    $haveWishSupport = true;
                    break;
                }
            }
            if (!$haveWishSupport) {
                return false;
            }
        }
        return true;
    }

    public static function ticketLevel($ticket) {
        $lastReply = end($ticket['replies']);
        switch ($lastReply['sender_id']) {
            case WishTicketSender::WISH_SUPPORT:
                return 'danger';
                break;
            case WishTicketSender::CUSTOMER:
                return 'warn';
                break;
            default:
                return 'safe';
                break;
        }
    }

    public static function emailTickets($tickets) {
        if (empty($tickets)) {
            // throw new Exception("no tickets");
            return -1;
        }

        $emailContent = "<html>";
        $emailContent = "可直接点击进入处理：<a href='http://www.wishwi.tk'>wishwi.tk</a>";
        $emailContent .= "<table>";

        $emailContent .= "<tr>";
        $emailContent .= "<th>TicketID</th>";
        $emailContent .= "<th>Level</th>";
        $emailContent .= "<th>OpenDate</th>";
        $emailContent .= "</tr>";
        foreach ($tickets as $ticketId => $ticket) {
            $emailContent .= "<tr>";
            $emailContent .= "<td><a href='http://merchant.wish.com/ticket/{$ticketId}' target='_blank'>{$ticketId}</a></td>";
            $emailContent .= "<td>".Helper::ticketLevel($ticket)."</td>";
            $emailContent .= "<td>{$ticket['open_date']}</td>";
            $emailContent .= "</tr>";
        }

        $emailContent .= "</table>";
        $emailContent .= "</html>";

        return Email::send('Ticket通知-'.date('Y-m-d H:i:s'), $emailContent, $GLOBALS['config']['MAIL_TICKET_TO']);
    }
}